package cn.com.pet.administrator.action.encyclopedia;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.pet.common.base.BaseAction;
import cn.com.pet.common.entity.PetInfo;
import cn.com.pet.common.service.PetInfoService;
import cn.com.pet.common.util.GuidUtil;
import cn.com.pet.common.util.StringUtil;

@Namespace("/action")
@ParentPackage("base")
@SuppressWarnings({ "serial", "unused" })
public class PetInfoAction extends BaseAction {

	@Autowired
	PetInfoService petInfoService;

	@Action(value = "pageAddPetInfo", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/addPetInfo.jsp") })
	public String pageAddPetInfo() {
		return SUCCESS;
	}

	@Action(value = "pageUpdatePetInfo", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/updatePetInfo.jsp") })
	public String pageUpdatePetInfo() {
		String petInfoId = this.getRequest().getParameter("id");
		this.getRequest().setAttribute("id", petInfoId);
		return SUCCESS;
	}

	@Action(value = "addPetInfo", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petInfoList.jsp") })
	public String addPetInfo() {

		PetInfo pi = new PetInfo();
		pi.setContent(this.getRequest().getParameter("content"));
		pi.setCreateDate(new Date());
		pi.setPetId(this.getRequest().getParameter("petId"));
		// pi.setPetInfoId(GuidUtil.createNewGuid(GuidUtil.FormatString));
		pi.setShowNo(StringUtil.parsInt(this.getRequest().getParameter("show")));
		pi.setStatus(1);
		pi.setTitle(this.getRequest().getParameter("title"));
		pi.setUpdateDate(new Date());

		int result = petInfoService.addPetInfo(pi);
		return SUCCESS;
	}

	@Action(value = "deletePetInfo", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petInfoList.jsp") })
	public String deletePetInfo() {

		PetInfo pi = new PetInfo();

		pi.setPetInfoId(StringUtil.parsLong(this.getRequest().getParameter("id")));

		int result = petInfoService.deletePetInfo(pi);

		return SUCCESS;
	}

	@Action(value = "updatePet", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petInfoList.jsp") })
	public String updatePet() {

		PetInfo pi = new PetInfo();
		pi.setContent(this.getRequest().getParameter("content"));
		pi.setPetId(this.getRequest().getParameter("petId"));
		pi.setPetInfoId(StringUtil.parsLong(this.getRequest().getParameter("petInfoId")));
		pi.setShowNo(StringUtil.parsInt(this.getRequest().getParameter("show")));
		pi.setTitle(this.getRequest().getParameter("title"));
		pi.setUpdateDate(new Date());

		int result = petInfoService.updatePetInfo(pi);

		return SUCCESS;
	}

	@Action(value = "findAllPetInfo", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petInfoList.jsp") })
	public String findAllPetInfo() {

		Map<String, String> parameter = new HashMap<String, String>();

		List<PetInfo> petInfoList = petInfoService.findAllPetInfo(parameter);
		this.getRequest().setAttribute("allPet", petInfoList);

		return SUCCESS;
	}

	@Action(value = "findPetInfoById", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petInfoDetail.jsp") })
	public String findPetInfoById() {

		PetInfo pi = new PetInfo();

		pi.setPetInfoId(StringUtil.parsLong(this.getRequest().getParameter("id")));

		PetInfo petInfo = petInfoService.findPetInfoeById(pi);

		this.getRequest().setAttribute("pet", petInfo);

		return SUCCESS;
	}
}
